import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '物料编号',		name: 'mate_number' },
			{ label: '物料名称',		name: 'mate_name' },
			{ label: '物料分类',		name: 'mate_type', 		type:'select', loading:true},
			{ label: '批次号',		name: 'batch_number' },
			{ label: '采购人',		name: 'buy_user' },
			{ label: '采购时间',		names: ['start_date','end_date'],	type:'date-range' },
		],
		// 提交表单列表
		formData : [
			{ label: '物料编号',		name: 'mate_number', 		half:true, 	required:true },
			{ label: '物料名称',		name: 'mate_name', 			half:true, 	required:true },
			{ label: '物料分类',		name: 'mate_type', 			half:true,	required:true, 	type:'select' },
			{ label: '审核人',		name: 'reviewer', 			half:true, 	required:true },
			{ label: '采购数量',		name: 'buy_qty', 			half:true, 	required:true },
			{ label: '采购人',		name: 'buy_user', 			half:true, 	required:true },
			{ label: '采购时间',		name: 'buy_date', 			half:true, 	required:true,type:'date-time' },
			{ label: '生产厂家',		name: 'mate_factory_id', 	half:true },
			{ label: '单位',			name: 'unit', 				half:true,	type:'select', 	required:true },
			{ label: '入库批次号',	name: 'batch_number', 		half:true, 	required:true },
			{ label: '保质期',		name: 'shelf_life', 		half:true, 	required:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	// 获取下拉数据
	getSelect = async () => {
		const { forms,formData } = this.state
		$cache.then( async f => {
			// 物料分类
			$fn.setRows([ forms, formData ], 'mate_type', await f.getDisCache({ code:26000 } ) )
			
			this.setState({ forms })
		})
	}
	// 获取弹框下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, [ 'unit' ], ()=>{
			$cache.then( async f => {
				// 单位
				$fn.setRow(formData, 'unit', await f.getDisCache({ code:22000 } ) )
				
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'material-in/index')
	
	// 表单
	cols = [
		{ title: '物料名称', 	dataIndex: 'mate_name', 		width:150 },
		{ title: '物料编号', 	dataIndex: 'mate_number', 		width:150 },
		{ title: '物料类型', 	dataIndex: 'mate_type_name', 	width:150 },
		{ title: '单位', 		dataIndex: 'unit_name', 		width:150 },
		{ title: '采购人', 		dataIndex: 'buy_user', 			...$fn.table.name },
		{ title: '审核人', 		dataIndex: 'reviewer', 			...$fn.table.name },
		{ title: '采购数量', 	dataIndex: 'buy_qty', 			width:150 },
		{ title: '批次号', 		dataIndex: 'batch_number', 		...$fn.table.num },
		{ title: '购买时间', 	dataIndex: 'buy_date', 			...$fn.table.time },
		{ title: '保质期', 		dataIndex: 'shelf_life', 		...$fn.table.num },
	]
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this, { click: this.getModalSelect }),
		]
	}
	
	render(){
		return (
			<>
				<Page title='物料入库批次列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { type: -1, isInit:window.isInit }) }
				</Page>
				{/* 添加 */}
				{ AddEditModal.call(this,{ labelWidth:'s7', width:800, api: 'material-in/add'}) }
			</>
		)
	}
}

export default Index